package com.davidlife.javase.netsocket.tcpdemo;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
    public static void main(String[] args) throws IOException {
        System.out.println("服务端启动,等待连接 .... ");
        ServerSocket ss = new ServerSocket(8888);
        Socket socket = ss.accept();
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        byte[] b = new byte[1024];
        //这里测试就只读一次
        int len = is.read(b);
        String msg = new String(b, 0, len);
        System.out.println("客户端发送了：" + msg);
        if (len != -1) {
            //服务端向客户端发送响应
            os.write("收到了".getBytes());
        }
        is.close();
        os.close();
        socket.close();
        ss.close();
    }
}
